﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class UserForgetPassword : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["username"] != null || Request["email"] != null)
            { 
                string username=Request["username"]??"";
                string email=Request["email"]??"";
                string secretkey=Request["secretkey"]??"";

                if (!IsValidKey(secretkey, username, email))
                {
                    Response.Write("112");//密钥不正确 
                    return;
                }

                Model.UserInfo user = new Model.UserInfo();
                if (!string.IsNullOrEmpty(username))
                {
                    user = BLL.UserInfoBLL.Instance.GetDetailByName(username);
                }
                else if (!string.IsNullOrEmpty(email))
                {
                    user = BLL.UserInfoBLL.Instance.GetDetailByEmail(email);
                }

                if (user != null)
                {
                    BLL.UserInfoBLL.Instance.SendRetrievePasswordEmail(user);
                    Response.Write(user.Email);//返回接收邮件的邮箱地址
                }
                else
                {
                    Response.Write("301");//无效的用户
                }
            }
            else
            {
                Response.Write("111");//参数不正确
            }
        }

        private bool IsValidKey(string secretkey, string username, string email)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + current_day + "name" + username + "email" + email).Substring(6, 22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}